﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1.API;
using Millionaire;

namespace WindowsFormsApplication1.BLL
{
    public class LinhVucBLL : BaseBLL,ILinhVucBLL
    {

        public bool AddNew(Millionaire.LinhVuc lv)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.LinhVucs.InsertOnSubmit(lv);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
               
            }
        }


        /// <summary>
        /// Ham cap nhat thong tin linh vuc
        /// </summary>
        /// <param name="lv">thong tin linh vuc cap nhat</param>
        /// <returns>true neu thanh cong false neu khong thanh cong</returns>
        public bool Update(Millionaire.LinhVuc lv)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LinhVuc edit = ctxt.LinhVucs.SingleOrDefault(x => x.MaLinhVuc == lv.MaLinhVuc);
                    if (edit != null)
                    {
                        edit.TenLinhVuc = lv.TenLinhVuc;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;
                  
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Delete(Millionaire.LinhVuc lv)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LinhVuc delObj = ctxt.LinhVucs.SingleOrDefault(x => x.MaLinhVuc == lv.MaLinhVuc);
                    if (delObj != null)
                    {

                        ctxt.LinhVucs.DeleteOnSubmit(delObj);
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public Millionaire.LinhVuc GetByID(int idLV)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                return ctxt.LinhVucs.SingleOrDefault(x=>x.MaLinhVuc == idLV);
            }
        }

        public List<Millionaire.LinhVuc> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LinhVucs.ToList();
            }
        }

        public Millionaire.LinhVuc GetByCauHoi(int idCauHoi)
        {
            throw new NotImplementedException();
        }
    }
}
